﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using InvoiceChecking.Entities;
using InvoiceChecking.Data;

#endregion

namespace InvoiceChecking.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="JournalVoucherProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class JournalVoucherProviderBaseCore : EntityProviderBase<InvoiceChecking.Entities.JournalVoucher, InvoiceChecking.Entities.JournalVoucherKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, InvoiceChecking.Entities.JournalVoucherKey key)
		{
			return Delete(transactionManager, key.JournalVoucherId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_journalVoucherId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.String _journalVoucherId)
		{
			return Delete(null, _journalVoucherId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.String _journalVoucherId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_JournalVoucher_JournalVoucherType key.
		///		FK_JournalVoucher_JournalVoucherType Description: 
		/// </summary>
		/// <param name="_journalVoucherTypeId"></param>
		/// <returns>Returns a typed collection of InvoiceChecking.Entities.JournalVoucher objects.</returns>
		public TList<JournalVoucher> GetByJournalVoucherTypeId(System.String _journalVoucherTypeId)
		{
			int count = -1;
			return GetByJournalVoucherTypeId(_journalVoucherTypeId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_JournalVoucher_JournalVoucherType key.
		///		FK_JournalVoucher_JournalVoucherType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherTypeId"></param>
		/// <returns>Returns a typed collection of InvoiceChecking.Entities.JournalVoucher objects.</returns>
		/// <remarks></remarks>
		public TList<JournalVoucher> GetByJournalVoucherTypeId(TransactionManager transactionManager, System.String _journalVoucherTypeId)
		{
			int count = -1;
			return GetByJournalVoucherTypeId(transactionManager, _journalVoucherTypeId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_JournalVoucher_JournalVoucherType key.
		///		FK_JournalVoucher_JournalVoucherType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherTypeId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of InvoiceChecking.Entities.JournalVoucher objects.</returns>
		public TList<JournalVoucher> GetByJournalVoucherTypeId(TransactionManager transactionManager, System.String _journalVoucherTypeId, int start, int pageLength)
		{
			int count = -1;
			return GetByJournalVoucherTypeId(transactionManager, _journalVoucherTypeId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_JournalVoucher_JournalVoucherType key.
		///		fkJournalVoucherJournalVoucherType Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_journalVoucherTypeId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of InvoiceChecking.Entities.JournalVoucher objects.</returns>
		public TList<JournalVoucher> GetByJournalVoucherTypeId(System.String _journalVoucherTypeId, int start, int pageLength)
		{
			int count =  -1;
			return GetByJournalVoucherTypeId(null, _journalVoucherTypeId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_JournalVoucher_JournalVoucherType key.
		///		fkJournalVoucherJournalVoucherType Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_journalVoucherTypeId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of InvoiceChecking.Entities.JournalVoucher objects.</returns>
		public TList<JournalVoucher> GetByJournalVoucherTypeId(System.String _journalVoucherTypeId, int start, int pageLength,out int count)
		{
			return GetByJournalVoucherTypeId(null, _journalVoucherTypeId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_JournalVoucher_JournalVoucherType key.
		///		FK_JournalVoucher_JournalVoucherType Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherTypeId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of InvoiceChecking.Entities.JournalVoucher objects.</returns>
		public abstract TList<JournalVoucher> GetByJournalVoucherTypeId(TransactionManager transactionManager, System.String _journalVoucherTypeId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override InvoiceChecking.Entities.JournalVoucher Get(TransactionManager transactionManager, InvoiceChecking.Entities.JournalVoucherKey key, int start, int pageLength)
		{
			return GetByJournalVoucherId(transactionManager, key.JournalVoucherId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_JournalVoucher index.
		/// </summary>
		/// <param name="_journalVoucherId"></param>
		/// <returns>Returns an instance of the <see cref="InvoiceChecking.Entities.JournalVoucher"/> class.</returns>
		public InvoiceChecking.Entities.JournalVoucher GetByJournalVoucherId(System.String _journalVoucherId)
		{
			int count = -1;
			return GetByJournalVoucherId(null,_journalVoucherId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_JournalVoucher index.
		/// </summary>
		/// <param name="_journalVoucherId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="InvoiceChecking.Entities.JournalVoucher"/> class.</returns>
		public InvoiceChecking.Entities.JournalVoucher GetByJournalVoucherId(System.String _journalVoucherId, int start, int pageLength)
		{
			int count = -1;
			return GetByJournalVoucherId(null, _journalVoucherId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_JournalVoucher index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="InvoiceChecking.Entities.JournalVoucher"/> class.</returns>
		public InvoiceChecking.Entities.JournalVoucher GetByJournalVoucherId(TransactionManager transactionManager, System.String _journalVoucherId)
		{
			int count = -1;
			return GetByJournalVoucherId(transactionManager, _journalVoucherId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_JournalVoucher index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="InvoiceChecking.Entities.JournalVoucher"/> class.</returns>
		public InvoiceChecking.Entities.JournalVoucher GetByJournalVoucherId(TransactionManager transactionManager, System.String _journalVoucherId, int start, int pageLength)
		{
			int count = -1;
			return GetByJournalVoucherId(transactionManager, _journalVoucherId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_JournalVoucher index.
		/// </summary>
		/// <param name="_journalVoucherId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="InvoiceChecking.Entities.JournalVoucher"/> class.</returns>
		public InvoiceChecking.Entities.JournalVoucher GetByJournalVoucherId(System.String _journalVoucherId, int start, int pageLength, out int count)
		{
			return GetByJournalVoucherId(null, _journalVoucherId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_JournalVoucher index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_journalVoucherId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="InvoiceChecking.Entities.JournalVoucher"/> class.</returns>
		public abstract InvoiceChecking.Entities.JournalVoucher GetByJournalVoucherId(TransactionManager transactionManager, System.String _journalVoucherId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region _JournalVoucher_Print 
		
		/// <summary>
		///	This method wrap the '_JournalVoucher_Print' stored procedure. 
		/// </summary>
		/// <param name="journalVoucherId"> A <c>System.String</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet Print(System.String journalVoucherId)
		{
			return Print(null, 0, int.MaxValue , journalVoucherId);
		}
		
		/// <summary>
		///	This method wrap the '_JournalVoucher_Print' stored procedure. 
		/// </summary>
		/// <param name="journalVoucherId"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet Print(int start, int pageLength, System.String journalVoucherId)
		{
			return Print(null, start, pageLength , journalVoucherId);
		}
				
		/// <summary>
		///	This method wrap the '_JournalVoucher_Print' stored procedure. 
		/// </summary>
		/// <param name="journalVoucherId"> A <c>System.String</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet Print(TransactionManager transactionManager, System.String journalVoucherId)
		{
			return Print(transactionManager, 0, int.MaxValue , journalVoucherId);
		}
		
		/// <summary>
		///	This method wrap the '_JournalVoucher_Print' stored procedure. 
		/// </summary>
		/// <param name="journalVoucherId"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public abstract DataSet Print(TransactionManager transactionManager, int start, int pageLength , System.String journalVoucherId);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;JournalVoucher&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;JournalVoucher&gt;"/></returns>
		public static TList<JournalVoucher> Fill(IDataReader reader, TList<JournalVoucher> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				InvoiceChecking.Entities.JournalVoucher c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("JournalVoucher")
					.Append("|").Append((System.String)reader[((int)JournalVoucherColumn.JournalVoucherId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<JournalVoucher>(
					key.ToString(), // EntityTrackingKey
					"JournalVoucher",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new InvoiceChecking.Entities.JournalVoucher();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.JournalVoucherId = (System.String)reader[((int)JournalVoucherColumn.JournalVoucherId - 1)];
					c.OriginalJournalVoucherId = c.JournalVoucherId;
					c.JournalVoucherDate = (System.DateTime)reader[((int)JournalVoucherColumn.JournalVoucherDate - 1)];
					c.JournalVoucherTypeId = (System.String)reader[((int)JournalVoucherColumn.JournalVoucherTypeId - 1)];
					c.Address = (reader.IsDBNull(((int)JournalVoucherColumn.Address - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.Address - 1)];
					c.Description = (reader.IsDBNull(((int)JournalVoucherColumn.Description - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.Description - 1)];
					c.ExchangeRate = (reader.IsDBNull(((int)JournalVoucherColumn.ExchangeRate - 1)))?null:(System.Decimal?)reader[((int)JournalVoucherColumn.ExchangeRate - 1)];
					c.PreparedBy = (reader.IsDBNull(((int)JournalVoucherColumn.PreparedBy - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.PreparedBy - 1)];
					c.AttachFile = (reader.IsDBNull(((int)JournalVoucherColumn.AttachFile - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.AttachFile - 1)];
					c.NotifyCode = (reader.IsDBNull(((int)JournalVoucherColumn.NotifyCode - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.NotifyCode - 1)];
					c.IsSentForApproval = (System.Boolean)reader[((int)JournalVoucherColumn.IsSentForApproval - 1)];
					c.IsApproved = (System.Boolean)reader[((int)JournalVoucherColumn.IsApproved - 1)];
					c.ApprovalBy = (System.String)reader[((int)JournalVoucherColumn.ApprovalBy - 1)];
					c.ApprovedDate = (reader.IsDBNull(((int)JournalVoucherColumn.ApprovedDate - 1)))?null:(System.DateTime?)reader[((int)JournalVoucherColumn.ApprovedDate - 1)];
					c.Reason = (reader.IsDBNull(((int)JournalVoucherColumn.Reason - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.Reason - 1)];
					c.IsCompleted = (System.Boolean)reader[((int)JournalVoucherColumn.IsCompleted - 1)];
					c.ExportedBy = (reader.IsDBNull(((int)JournalVoucherColumn.ExportedBy - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.ExportedBy - 1)];
					c.ExportedDate = (reader.IsDBNull(((int)JournalVoucherColumn.ExportedDate - 1)))?null:(System.DateTime?)reader[((int)JournalVoucherColumn.ExportedDate - 1)];
					c.IsDebit = (reader.IsDBNull(((int)JournalVoucherColumn.IsDebit - 1)))?null:(System.Boolean?)reader[((int)JournalVoucherColumn.IsDebit - 1)];
					c.IsVoid = (System.Boolean)reader[((int)JournalVoucherColumn.IsVoid - 1)];
					c.VoidReason = (reader.IsDBNull(((int)JournalVoucherColumn.VoidReason - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.VoidReason - 1)];
					c.VoidBy = (reader.IsDBNull(((int)JournalVoucherColumn.VoidBy - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.VoidBy - 1)];
					c.VoidDate = (reader.IsDBNull(((int)JournalVoucherColumn.VoidDate - 1)))?null:(System.DateTime?)reader[((int)JournalVoucherColumn.VoidDate - 1)];
					c.CreatedDate = (System.DateTime)reader[((int)JournalVoucherColumn.CreatedDate - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="InvoiceChecking.Entities.JournalVoucher"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="InvoiceChecking.Entities.JournalVoucher"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, InvoiceChecking.Entities.JournalVoucher entity)
		{
			if (!reader.Read()) return;
			
			entity.JournalVoucherId = (System.String)reader[((int)JournalVoucherColumn.JournalVoucherId - 1)];
			entity.OriginalJournalVoucherId = (System.String)reader["JournalVoucherId"];
			entity.JournalVoucherDate = (System.DateTime)reader[((int)JournalVoucherColumn.JournalVoucherDate - 1)];
			entity.JournalVoucherTypeId = (System.String)reader[((int)JournalVoucherColumn.JournalVoucherTypeId - 1)];
			entity.Address = (reader.IsDBNull(((int)JournalVoucherColumn.Address - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.Address - 1)];
			entity.Description = (reader.IsDBNull(((int)JournalVoucherColumn.Description - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.Description - 1)];
			entity.ExchangeRate = (reader.IsDBNull(((int)JournalVoucherColumn.ExchangeRate - 1)))?null:(System.Decimal?)reader[((int)JournalVoucherColumn.ExchangeRate - 1)];
			entity.PreparedBy = (reader.IsDBNull(((int)JournalVoucherColumn.PreparedBy - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.PreparedBy - 1)];
			entity.AttachFile = (reader.IsDBNull(((int)JournalVoucherColumn.AttachFile - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.AttachFile - 1)];
			entity.NotifyCode = (reader.IsDBNull(((int)JournalVoucherColumn.NotifyCode - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.NotifyCode - 1)];
			entity.IsSentForApproval = (System.Boolean)reader[((int)JournalVoucherColumn.IsSentForApproval - 1)];
			entity.IsApproved = (System.Boolean)reader[((int)JournalVoucherColumn.IsApproved - 1)];
			entity.ApprovalBy = (System.String)reader[((int)JournalVoucherColumn.ApprovalBy - 1)];
			entity.ApprovedDate = (reader.IsDBNull(((int)JournalVoucherColumn.ApprovedDate - 1)))?null:(System.DateTime?)reader[((int)JournalVoucherColumn.ApprovedDate - 1)];
			entity.Reason = (reader.IsDBNull(((int)JournalVoucherColumn.Reason - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.Reason - 1)];
			entity.IsCompleted = (System.Boolean)reader[((int)JournalVoucherColumn.IsCompleted - 1)];
			entity.ExportedBy = (reader.IsDBNull(((int)JournalVoucherColumn.ExportedBy - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.ExportedBy - 1)];
			entity.ExportedDate = (reader.IsDBNull(((int)JournalVoucherColumn.ExportedDate - 1)))?null:(System.DateTime?)reader[((int)JournalVoucherColumn.ExportedDate - 1)];
			entity.IsDebit = (reader.IsDBNull(((int)JournalVoucherColumn.IsDebit - 1)))?null:(System.Boolean?)reader[((int)JournalVoucherColumn.IsDebit - 1)];
			entity.IsVoid = (System.Boolean)reader[((int)JournalVoucherColumn.IsVoid - 1)];
			entity.VoidReason = (reader.IsDBNull(((int)JournalVoucherColumn.VoidReason - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.VoidReason - 1)];
			entity.VoidBy = (reader.IsDBNull(((int)JournalVoucherColumn.VoidBy - 1)))?null:(System.String)reader[((int)JournalVoucherColumn.VoidBy - 1)];
			entity.VoidDate = (reader.IsDBNull(((int)JournalVoucherColumn.VoidDate - 1)))?null:(System.DateTime?)reader[((int)JournalVoucherColumn.VoidDate - 1)];
			entity.CreatedDate = (System.DateTime)reader[((int)JournalVoucherColumn.CreatedDate - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="InvoiceChecking.Entities.JournalVoucher"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="InvoiceChecking.Entities.JournalVoucher"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, InvoiceChecking.Entities.JournalVoucher entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.JournalVoucherId = (System.String)dataRow["JournalVoucherId"];
			entity.OriginalJournalVoucherId = (System.String)dataRow["JournalVoucherId"];
			entity.JournalVoucherDate = (System.DateTime)dataRow["JournalVoucherDate"];
			entity.JournalVoucherTypeId = (System.String)dataRow["JournalVoucherTypeId"];
			entity.Address = Convert.IsDBNull(dataRow["Address"]) ? null : (System.String)dataRow["Address"];
			entity.Description = Convert.IsDBNull(dataRow["Description"]) ? null : (System.String)dataRow["Description"];
			entity.ExchangeRate = Convert.IsDBNull(dataRow["ExchangeRate"]) ? null : (System.Decimal?)dataRow["ExchangeRate"];
			entity.PreparedBy = Convert.IsDBNull(dataRow["PreparedBy"]) ? null : (System.String)dataRow["PreparedBy"];
			entity.AttachFile = Convert.IsDBNull(dataRow["AttachFile"]) ? null : (System.String)dataRow["AttachFile"];
			entity.NotifyCode = Convert.IsDBNull(dataRow["NotifyCode"]) ? null : (System.String)dataRow["NotifyCode"];
			entity.IsSentForApproval = (System.Boolean)dataRow["IsSentForApproval"];
			entity.IsApproved = (System.Boolean)dataRow["IsApproved"];
			entity.ApprovalBy = (System.String)dataRow["ApprovalBy"];
			entity.ApprovedDate = Convert.IsDBNull(dataRow["ApprovedDate"]) ? null : (System.DateTime?)dataRow["ApprovedDate"];
			entity.Reason = Convert.IsDBNull(dataRow["Reason"]) ? null : (System.String)dataRow["Reason"];
			entity.IsCompleted = (System.Boolean)dataRow["IsCompleted"];
			entity.ExportedBy = Convert.IsDBNull(dataRow["ExportedBy"]) ? null : (System.String)dataRow["ExportedBy"];
			entity.ExportedDate = Convert.IsDBNull(dataRow["ExportedDate"]) ? null : (System.DateTime?)dataRow["ExportedDate"];
			entity.IsDebit = Convert.IsDBNull(dataRow["IsDebit"]) ? null : (System.Boolean?)dataRow["IsDebit"];
			entity.IsVoid = (System.Boolean)dataRow["IsVoid"];
			entity.VoidReason = Convert.IsDBNull(dataRow["VoidReason"]) ? null : (System.String)dataRow["VoidReason"];
			entity.VoidBy = Convert.IsDBNull(dataRow["VoidBy"]) ? null : (System.String)dataRow["VoidBy"];
			entity.VoidDate = Convert.IsDBNull(dataRow["VoidDate"]) ? null : (System.DateTime?)dataRow["VoidDate"];
			entity.CreatedDate = (System.DateTime)dataRow["CreatedDate"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="InvoiceChecking.Entities.JournalVoucher"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">InvoiceChecking.Entities.JournalVoucher Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, InvoiceChecking.Entities.JournalVoucher entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region JournalVoucherTypeIdSource	
			if (CanDeepLoad(entity, "JournalVoucherType|JournalVoucherTypeIdSource", deepLoadType, innerList) 
				&& entity.JournalVoucherTypeIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.JournalVoucherTypeId;
				JournalVoucherType tmpEntity = EntityManager.LocateEntity<JournalVoucherType>(EntityLocator.ConstructKeyFromPkItems(typeof(JournalVoucherType), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.JournalVoucherTypeIdSource = tmpEntity;
				else
					entity.JournalVoucherTypeIdSource = DataRepository.JournalVoucherTypeProvider.GetByJournalVoucherTypeId(transactionManager, entity.JournalVoucherTypeId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'JournalVoucherTypeIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.JournalVoucherTypeIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.JournalVoucherTypeProvider.DeepLoad(transactionManager, entity.JournalVoucherTypeIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion JournalVoucherTypeIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByJournalVoucherId methods when available
			
			#region JournalDetailsCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<JournalDetails>|JournalDetailsCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'JournalDetailsCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.JournalDetailsCollection = DataRepository.JournalDetailsProvider.GetByJournalVoucherId(transactionManager, entity.JournalVoucherId);

				if (deep && entity.JournalDetailsCollection.Count > 0)
				{
					deepHandles.Add("JournalDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<JournalDetails>) DataRepository.JournalDetailsProvider.DeepLoad,
						new object[] { transactionManager, entity.JournalDetailsCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the InvoiceChecking.Entities.JournalVoucher object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">InvoiceChecking.Entities.JournalVoucher instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">InvoiceChecking.Entities.JournalVoucher Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, InvoiceChecking.Entities.JournalVoucher entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region JournalVoucherTypeIdSource
			if (CanDeepSave(entity, "JournalVoucherType|JournalVoucherTypeIdSource", deepSaveType, innerList) 
				&& entity.JournalVoucherTypeIdSource != null)
			{
				DataRepository.JournalVoucherTypeProvider.Save(transactionManager, entity.JournalVoucherTypeIdSource);
				entity.JournalVoucherTypeId = entity.JournalVoucherTypeIdSource.JournalVoucherTypeId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<JournalDetails>
				if (CanDeepSave(entity.JournalDetailsCollection, "List<JournalDetails>|JournalDetailsCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(JournalDetails child in entity.JournalDetailsCollection)
					{
						if(child.JournalVoucherIdSource != null)
						{
							child.JournalVoucherId = child.JournalVoucherIdSource.JournalVoucherId;
						}
						else
						{
							child.JournalVoucherId = entity.JournalVoucherId;
						}

					}

					if (entity.JournalDetailsCollection.Count > 0 || entity.JournalDetailsCollection.DeletedItems.Count > 0)
					{
						//DataRepository.JournalDetailsProvider.Save(transactionManager, entity.JournalDetailsCollection);
						
						deepHandles.Add("JournalDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< JournalDetails >) DataRepository.JournalDetailsProvider.DeepSave,
							new object[] { transactionManager, entity.JournalDetailsCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region JournalVoucherChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>InvoiceChecking.Entities.JournalVoucher</c>
	///</summary>
	public enum JournalVoucherChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>JournalVoucherType</c> at JournalVoucherTypeIdSource
		///</summary>
		[ChildEntityType(typeof(JournalVoucherType))]
		JournalVoucherType,
		///<summary>
		/// Collection of <c>JournalVoucher</c> as OneToMany for JournalDetailsCollection
		///</summary>
		[ChildEntityType(typeof(TList<JournalDetails>))]
		JournalDetailsCollection,
	}
	
	#endregion JournalVoucherChildEntityTypes
	
	#region JournalVoucherFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;JournalVoucherColumn&gt;"/> class
	/// that is used exclusively with a <see cref="JournalVoucher"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class JournalVoucherFilterBuilder : SqlFilterBuilder<JournalVoucherColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the JournalVoucherFilterBuilder class.
		/// </summary>
		public JournalVoucherFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the JournalVoucherFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public JournalVoucherFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the JournalVoucherFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public JournalVoucherFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion JournalVoucherFilterBuilder
	
	#region JournalVoucherParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;JournalVoucherColumn&gt;"/> class
	/// that is used exclusively with a <see cref="JournalVoucher"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class JournalVoucherParameterBuilder : ParameterizedSqlFilterBuilder<JournalVoucherColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the JournalVoucherParameterBuilder class.
		/// </summary>
		public JournalVoucherParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the JournalVoucherParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public JournalVoucherParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the JournalVoucherParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public JournalVoucherParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion JournalVoucherParameterBuilder
	
	#region JournalVoucherSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;JournalVoucherColumn&gt;"/> class
	/// that is used exclusively with a <see cref="JournalVoucher"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class JournalVoucherSortBuilder : SqlSortBuilder<JournalVoucherColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the JournalVoucherSqlSortBuilder class.
		/// </summary>
		public JournalVoucherSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion JournalVoucherSortBuilder
	
} // end namespace
